﻿Friend Class UnitTestCore
    Private Shared ReadOnly username As String = "dotnetnuke"
    Private Shared ReadOnly password As String = "dotnetnuke"
    Private Shared ReadOnly database As String = "DotNetNukeGeminorum"
    Private Shared ReadOnly server As String = "."
    Private Shared ReadOnly appName As String = "Unit Tester"

    Public Shared Sub ExecuteSQLNonQuery(SQL As String)
        Using db As New GeminorumSoftware.Database.SQLDatabase
            db.OpenConnection(New GeminorumSoftware.Database.ConnectionString(username, password, database, server, appName))
            db.SetSQLParameters(SQL, CommandType.Text)
            db.ExecuteNONQuery()
        End Using
    End Sub

    Public Shared Function ExecuteDataTableQuery(SQL As String) As DataTable
        Dim dt As DataTable

        Using db As New GeminorumSoftware.Database.SQLDatabase
            db.OpenConnection(New GeminorumSoftware.Database.ConnectionString(username, password, database, server, appName))
            db.SetSQLParameters(SQL, CommandType.Text)
            dt = db.ExecuteReader()
        End Using

        Return dt
    End Function

    Public Shared Function DatabaseTableExists(ObjectName As String) As Boolean
        Dim GetCountOfTableObject As String = "SELECT Count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.<TableName>') AND type in (N'U')"
        Return ExecuteDataTableQuery(GetCountOfTableObject.Replace("<TableName>", ObjectName)).Rows(0).Item(0) > 0
    End Function

    Public Shared Function StoredProcedureExists(ObjectName As String) As Boolean
        Dim GetCountOfStoredProcedure As String = "SELECT Count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.<StoredProcedureName>') AND type in (N'P', N'PC')"
        Return ExecuteDataTableQuery(GetCountOfStoredProcedure.Replace("<StoredProcedureName>", ObjectName)).Rows(0).Item(0) > 0
    End Function
End Class